<?php

namespace app\admin\controller;

use think\Controller;
use think\facade\Request;
use \app\common\model\Goods as Goodslist;
use think\facade\Session;
class Goods extends Controller
{
    
    public function index()
    {
        $page=1;
        $count=Goodslist::count();
        $ye=ceil($count/3);

        
         $data=Goodslist::page($page,3)->select();
        // dump($data);
        //渲染模板，传递数据    assgin
        return $this->fetch('index',['list'=>$data,'page'=>$page,'ye'=>$ye,'count'=>$count]);
    }


    // public function search(){

    //     $param=Request::param('id');
    //     // echo $param;
    //     $data=Goodslist::where('id','like',"%$param%")->select();
    //     return $this->fetch('index',['list'=>$data]);
       
    // }
    public function goodsadd(){
        // echo "新增";
        return $this->fetch('goodsadd');
    }

    public function upload()
    {
        $file=request()->file('goods_logo');
        $info=$file->move('../public/uploads');
        if($info){
            return $img="/uploads/".$info->getSaveName();

        }else{
            $this->error('上传失败');
        }
    }
    // public function save()
    // {
    //     $data=Request::param();
    //     $data["goods_logo"]=$this->upload();
    //     Goodslist::create($data,true);
    //     $this->redirect('goods');
    // }
    public function save(){
        $data=Request::param();
        $data["goods_logo"]=$this->upload();

        $rule=[
            'id|编号'=>'require',
            'goods_name|商品名称'=>'require',
            'goods_price|商品价格'=>'require',
            'goods_logo|图片'=>'require',
            'goods_number|数量'=>'require',
            

        ];
        $message=[
            'id'=>'编号不能为空',
            'goods_name'=>'商品名不能为空',
            'goods_price'=>'价格不能为空',
            'goods_logo'=>'图片不能为空',
            'goods_number'=>'数量不能为空',

        ];
        $validate=new \think\Validate($rule,$message);
        if($validate->check($data)){

            if(Goodslist::insert(['id'=>$data['id'],'goods_name'=>$data['goods_name'],'goods_price'=>$data['goods_price'],'goods_logo'=>$data['goods_logo'],'goods_number'=>$data['goods_number']])){
                $this->success('添加成功','/admin/goods/index');
            }else{
                $this->error('添加失败','/admin/goods/index');
            }
        }else{
            $this->error($validate->getError(),'admin/index/index');
        }
        
        // dump($data);
        // Businesslist::create($data,true);
        // $this->redirect('businessadd');



        
    }
    public function delect($id){
        $id=Request::param();
        $id=$id['id'];
        if(Goodslist::where('id',$id)->delete()){
            $this->success('删除成功','/admin/goods/index');
        }else{
                $this->error('删除失败','/admin/goods/index');
        }
    }

    public function goodsedit(){
        $id=Request::param();
       
        $id=$id['id'];
        $data=Goodslist::where('id',$id)->find();
        // dump($data);
        return $this->fetch('goodsedit',['data'=>$data]);
    }

    public function goodsed($id){
        $data=Request::param();
         $file=request()->file('goods_logo');

         if(!empty($file)){
             $data['goods_logo']=$this->upload();
             $goods=Goods::find($id);

         }
        $rule=[
            'id|编号'=>'require',
            'goods_name|商品名称'=>'require',
            'goods_price|商品价格'=>'require',
            'goods_logo|图片'=>'require',
            'goods_number|数量'=>'require',
            

        ];
        $message=[
            'id'=>'编号不能为空',
            'goods_name'=>'商品名不能为空',
            'goods_price'=>'价格不能为空',
            'goods_logo'=>'图片不能为空',
            'goods_number'=>'数量不能为空',

        ];
        $validate=new \think\Validate($rule,$message);
        if($validate->check($data)){
            $result=[
                'id'=>$data['id'],
                'goods_name'=>$data['goods_name'],
                'goods_price'=>$data['goods_price'],
                'goods_logo'=>$data['goods_logo'],
                'goods_number'=>$data['goods_number'],
            ];
            if(Goodslist::where('id',$data['id'])->update($result)){
                $this->success('修改成功','/admin/goods/index');
            }else{
                // dump($res);
                $this->error('修改失败','/admin/goods/index'); 
            }
        }else{
            $this->error($validate->getError(),'admin/goods/index');
        }
    }

    public function prev(){
        $page=Request::param();
        $page=(int)$page['page'];
        $page=$page-1;
        $page=$page<1?1:$page;
        $count= Goodslist::count();
        $ye=ceil($count/3);
        $data= Goodslist::page($page,3)->select();
        return $this->fetch('index',['list'=>$data,'page'=>$page,'ye'=>$ye,'count'=>$count]);
   }
   public function next(){
       $page=Request::param();     
        $page=(int)$page['page'];
        $page=$page+1;
        $count= Goodslist::count();
        $ye=ceil($count/3);
        $page=$page>$ye?$ye:$page;
        $data= Goodslist::page($page,3)->select();
        return $this->fetch('index',['list'=>$data,'page'=>$page,'ye'=>$ye,'count'=>$count]);
   }  
    
}
